package com.easondongh.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easondongh.system.constants.Constants;
import com.easondongh.system.utils.StringUtils;
import com.easondongh.common.vo.BasePageQueryVo;
import com.easondongh.system.vo.NoticeQueryVo;
import com.easondongh.common.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easondongh.system.mapper.SysNoticeMapper;
import com.easondongh.system.domain.SysNotice;
import com.easondongh.system.service.SysNoticeService;

/**
 * @author EasonDongH
 * @date 2020/5/4 20:14
 */
@Service
public class SysNoticeServiceImpl extends ServiceImpl<SysNoticeMapper, SysNotice> implements SysNoticeService {

    @Autowired
    private SysNoticeMapper noticeMapper;

    @Override
    public PageResult<SysNotice> getByPage(BasePageQueryVo<NoticeQueryVo> queryCondition) {
        IPage<SysNotice> page = new Page<>(queryCondition.getPage(), queryCondition.getLimit());
        QueryWrapper<SysNotice> queryWrapper = new QueryWrapper<>();
        NoticeQueryVo queryParams = queryCondition.getQueryParams();
        if (queryParams != null) {
            queryWrapper.like(StringUtils.isNotBlank(queryParams.getTitle()), SysNotice.COL_TITLE, queryParams.getTitle());
            queryWrapper.like(StringUtils.isNotBlank(queryParams.getContent()), SysNotice.COL_CONTENT, queryParams.getContent());
            queryWrapper.like(StringUtils.isNotBlank(queryParams.getCreateBy()), SysNotice.COL_CREATE_BY, queryParams.getCreateBy());
        }
        queryWrapper.orderByDesc(SysNotice.COL_CREATE_TIME);
        IPage<SysNotice> iPage = this.noticeMapper.selectPage(page, queryWrapper);
        return new PageResult<>(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), iPage.getRecords());
    }

    @Override
    public boolean enableById(Integer id) {
        SysNotice notice = new SysNotice();
        notice.setId(id);
        notice.setEnable(Constants.ENABLE);
        return this.noticeMapper.updateById(notice) == 1;
    }

    @Override
    public boolean disableById(Integer id) {
        SysNotice notice = new SysNotice();
        notice.setId(id);
        notice.setEnable(Constants.DISABLE);
        return this.noticeMapper.updateById(notice) == 1;
    }

    @Override
    public List<SysNotice> showNotices(Integer limit) {
        QueryWrapper<SysNotice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SysNotice.COL_ENABLE, Constants.ENABLE);
        queryWrapper.orderByDesc(SysNotice.COL_CREATE_TIME);
        IPage<SysNotice> page = new Page<>(1, limit);
        IPage<SysNotice> iPage = this.noticeMapper.selectPage(page, queryWrapper);
        return iPage.getRecords();
    }
}

